package leetcode.str;

public class Test05 {

    public static void main(String[] args) {
        String hello_world = replaceSpace("Hello World ");
        System.out.println(hello_world);
    }

//    请实现一个函数，把字符串 s 中的每个空格替换成"%20"。
    public static String replaceSpace(String s) {
        char[] chars = s.toCharArray();
        int length = chars.length;
        for (char aChar : chars) {
            if (aChar == ' '){
                length+=2;
            }
        }
        char[] r = new char[length];
        int j = 0;
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == ' '){
                r[j] = '%';
                r[j+1] = '2';
                r[j+2] = '0';
                j+=3;
            }
            else{
                r[j] = chars[i];
                j++;
            }
        }
        return new String(r);
    }
}
